package com.jch.dm12_half_sync_half_async.example;

import com.jch.dm2_guarded_suspension.reusable_code.Blocker;
import com.jch.dm2_guarded_suspension.reusable_code.ConditionVarBlocker;
import com.jch.dm2_guarded_suspension.reusable_code.GuardedAction;
import com.jch.dm2_guarded_suspension.reusable_code.Predicate;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Callable;

public class AlarmAgent {

    private volatile boolean connectedToServer = false;

    // 角色 GuardedSuspension.Predicate
    private final Predicate agentConnected = new Predicate() {
        @Override
        public boolean evaluate() {
            return connectedToServer;
        }
    };

    // 角色 GuardedSuspension.Blocker
    private final Blocker blocker = new ConditionVarBlocker();

    // 心跳定时器
    private final Timer heartbeatTimer = new Timer(true);

    // 发送告警信息
    public void sendAlarm(final AlarmInfo alarm) throws Exception{
        // 角色 GuardedSuspension.GuardedAction
        GuardedAction<Void> guardedAction = new GuardedAction<Void>(agentConnected) {
            @Override
            public Void call() throws Exception {
                doSendAlarm(alarm);
                return null;
            }
        };
        blocker.callWithGuard(guardedAction);
    }

    private void doSendAlarm(AlarmInfo alarm){
        System.out.println("sending alarm " + alarm);
        try{
            // 模拟耗时
            Thread.sleep(50);
        } catch (InterruptedException e) {

        }
    }

    public void init(){
        Thread connectingThread = new Thread(new ConnectingTask());
        connectingThread.start();
        heartbeatTimer.schedule(new HeartbeatTask(),60000,2000);
    }

    public void disconnect(){
        System.out.println("disconnect from alarm server.");
        connectedToServer = false;
    }

    protected void onConnected(){
        try{
            blocker.signalAfter(new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception {
                    connectedToServer = true;
                    System.out.println("connected to server");
                    return Boolean.TRUE;
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    protected void onDisconnected(){
        connectedToServer = false;
    }

    private class ConnectingTask implements Runnable{

        @Override
        public void run() {
            // 模拟耗时
            try{
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
            onConnected();
        }
    }

    private class HeartbeatTask extends TimerTask{

        @Override
        public void run() {
            if (!testConnection()){
                onDisconnected();
                reconnect();
            }
        }

        private boolean testConnection(){
            return true;
        }

        private void reconnect(){
            ConnectingTask connectingThread = new ConnectingTask();
            connectingThread.run();
        }
    }

}
